package edu.hrbu.trade01.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.hrbu.trade01.annotation.MyLog;
import edu.hrbu.trade01.entity.Commence;
import edu.hrbu.trade01.service.CommenceService;
import edu.hrbu.trade01.util.R;
import edu.hrbu.trade01.util.TableR;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static edu.hrbu.trade01.util.ConstUtil.*;

/**
 * @author 徐登宇
 */
@RestController
@RequestMapping("/commence")
public class CommenceController {

    @Resource(name = "commenceServiceImpl")
    private CommenceService commenceService;

    // 条件分页查询
    @RequestMapping("/list")
    public TableR list(@RequestParam(value = "page", defaultValue = "1") int page,
                             @RequestParam(value = "limit", defaultValue = "5") int limit,
                             @RequestParam(value = "searchParams", required = false) String commenceStr) {

        Page<Commence> pageModel = commenceService.getPageWithCondition(page, limit, commenceStr);
//        log.info("@"+pageModel);
        return TableR.ok(pageModel.getTotal(), pageModel.getRecords());
    }

    @MyLog(title = LOG_TITLE_SAVE, businessType = LOG_TYPE_PROFESSION)
    @PostMapping("/save")
    public R save(@RequestBody Commence commence) {
         commenceService.save(commence);
         return R.ok();
    }

    @MyLog(title = LOG_TITLE_UPDATE, businessType = LOG_TYPE_PROFESSION)
    @PostMapping("/update")
    public R update(@RequestBody Commence commence){
        commenceService.updateById(commence);
        return R.ok();
    }

    @MyLog(title = LOG_TITLE_DEL, businessType = LOG_TYPE_PROFESSION)
    @PostMapping("/remove")
    public R remove(@RequestParam("id") Long id){
        commenceService.removeById(id);
        return R.ok();
    }

    @MyLog(title = "批量删除", businessType = LOG_TYPE_PROFESSION)
    @PostMapping("/removeBatch")
    public R removeBatch(@RequestParam("ids") List<Long> ids) {
        commenceService.removeBatchByIds(ids);
        return R.ok();
    }

}
